{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "def longest_palindrome(string):\n",
    "    length=len(string)#length of the parent string\n",
    "    \n",
    "    palindromes_length=[1]*length\n",
    "    \n",
    "    for fgap in range(1,length):#iterate on each substring\n",
    "        \n",
    "        pre=palindromes_length[fgap]\n",
    "        for rgap in reversed(range(0,fgap)):\n",
    "            tmp=palindromes_length[rgap]\n",
    "            if string[fgap]==string[rgap]:\n",
    "                if rgap+1>fgap-1:\n",
    "                    palindromes_length[rgap]=2\n",
    "                else:\n",
    "                    palindromes_length[rgap]=2+pre\n",
    "            else:\n",
    "                palindromes_length[rgap]=max(\n",
    "                    palindromes_length[rgap+1],palindromes_length[rgap]\n",
    "                )\n",
    "            pre=tmp\n",
    "    return palindromes_length[0]\n",
    "\n",
    "\n",
    "print(longest_palindrome(\"bbabcbcab\"))\n",
    "print(longest_palindrome(\"abbaab\"))\n",
    "print(longest_palindrome(\"opengenus\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<function __main__.main>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "main"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7\n",
      "4\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "__main__()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
